﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Warehouse.DataAccess
{
    public abstract class BaseDA<T> where T : class
    {
        protected abstract T getEntity(IDataReader reader);

        protected T getSingle(Database db, DbCommand cmd)
        {
            var reader = db.ExecuteReader(cmd);
            T user = null;
            try
            {
                if (reader.Read())
                {
                    user = getEntity(reader);
                }
            }
            finally
            {
                reader.Close();
            }

            return user;
        }

        protected IEnumerable<T> getMany(Database db, DbCommand cmd)
        {
            var reader = db.ExecuteReader(cmd);
            var list = new List<T>();
            try
            {
                while (reader.Read())
                {
                    list.Add(getEntity(reader));
                }
            }
            finally
            {
                reader.Close();
            }
            return list;
        }
    }
}
